//
//  UserInfo.swift
//  WBSwift
//
//  Created by maker on 2016/11/12.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit

class UserInfo: NSObject,NSCoding{
    //MARK:==========属性==========
    var access_token : String?
    var expires_in : TimeInterval = 0.0 {
        
        didSet{
           expires_date = Date(timeIntervalSinceNow: expires_in)
        }
    }
    var uid : String?           //用户 id
    var expires_date :Date?   //过期时间
    var screen_name :String?    //用户昵称
    var avatar_large :String?   //用户头像
    
    
    

    //构造方法
    init(dict : [String:AnyObject]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    /*
     [打印对象]
        默认:打印对象的名字,和内存地址
        如果想打印对象的时候,把对象的属性也打印出来.就要重写对象的description 属性.返回一个字符串
     */
    override var description: String{
        return dictionaryWithValues(forKeys: ["access_token","expires_date","uid","screen_name","avatar_large"]).description
    }
    

    //1.解档方法
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
    }
    
    //2.归档方法
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_date, forKey: "expires_date")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
    
}

